package com.jieshuibao.wx.util;

import java.io.IOException;
import java.nio.charset.UnsupportedCharsetException;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.jieshuibao.wx.po.AccessToken;

@SuppressWarnings("deprecation")
public class WeiXinPublicUtil {
	/**
	 * 请求来源  APP / 公众号
	 */
	public static final String SRCAPP = "APP";
	public static final String SRCMP = "JSAPI";
	//网站APP
//	public static final String APPID = "wx861066cf5373617f";
//	public static final String APPSECRET = "3b9b6f9fb3ec1a9aec440f4e87f067fa";
	//公众号
	public static final String PUBLICAPPID = "wx64eaa52e2ff9ec97";
	public static final String PUBLICMCHID = "1358458902";
	public static final String PUBLICAPPSECRET = "91a820cf58677b87ad9edfb86fbf1bb8";
	public static final String MP_API_KEY = "";
	public static final String TOKEN = "jieshuibaocom1128";
	public static final String EncodingAESKey = "929WchIVXH9h49p7ErjKAS91Q2R5OMJROUzGjxryPbg";
	public static final String ACCESSTOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
	
	@SuppressWarnings({ "resource" })
	public static JSONObject doGet(String url){
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		JSONObject jsonObj = null;
		try {
			HttpResponse response = httpClient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			if(null != entity){
				String result = EntityUtils.toString(entity, "UTF-8");
				jsonObj = JSONObject.fromObject(result);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return jsonObj;
	}
	@SuppressWarnings({ "resource" })
	public static JSONObject doPost(String url,String str){
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		JSONObject jsonObj = null;
		try {
			httpPost.setEntity(new StringEntity(str, "UTF-8"));
			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			if(null != entity){
				String result = EntityUtils.toString(entity, "UTF-8");
				jsonObj = JSONObject.fromObject(result);
			}
		} catch (UnsupportedCharsetException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return jsonObj;
	}
	/**
	 * 获取微信accessToken
	 * @return
	 */
	public static AccessToken getAccessToken(){
		AccessToken accessToken = new AccessToken();
		String url = ACCESSTOKEN_URL.replace("APPID", PUBLICAPPID).replace("APPSECRET", PUBLICAPPSECRET);
		JSONObject jsonObj = doGet(url);
		if(null != jsonObj){
			accessToken.setToken(jsonObj.getString("access_token"));
			accessToken.setExpiresin(jsonObj.getInt("expires_in"));
		}
		return accessToken;
	}
}
